﻿/*
 *  Copyright 2012 HEINER DIRECT GmbH & Co. KG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 * @Author Rafael Dabrowski
 * 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace HDI.SharePoint.Webparts.HDIAggregatedView.HDIAggregatedViewWP
{
    class HDIAggregatedViewEditorPart: EditorPart
    {
        protected HDIAggregatedViewWP ParentWebPart { get; set; }
        protected TextBox JSON { get; set; }
       



        public HDIAggregatedViewEditorPart()
        {
            Title = "Konfiguration";
        }

        protected override void CreateChildControls()
        {
            JSON = new TextBox();
            JSON.CssClass = "JSON";

            ReferencePrevalidatedScript("$.colorbox", Config.resourcePath + "js/colorbox.js");
            Controls.Add(new LiteralControl("<script type='text/javascript'>$(function(){$('.colorbox').colorbox();});</script>"));
            Controls.Add(new LiteralControl("<a href=" + Config.resourcePath + "ConfigForm.htm?" + DateTime.Now.ToFileTime() + "' title='HDITable Konfiguration' class='colorbox'>KONFIGURIEREN</a>"));


            Page.Header.Controls.Add(new LiteralControl(String.Format("<link  href='{0}' rel='stylesheet' type='text/css' />", Config.resourcePath + "css/style.css?" + DateTime.Now.ToFileTimeUtc())));

            ReferencePrevalidatedScript("ko", Config.resourcePath + "js/knockout.js");
            ReferencePrevalidatedScript("xcv", Config.resourcePath + "js/jQueryUI.js");
            ReferencePrevalidatedScript("qTip", Config.resourcePath + "js/qtip.js");

            ReferencePrevalidatedScript("HDIAggregatedView", Config.resourcePath + "js/Config.js?" + DateTime.Now.ToFileTimeUtc());

            Controls.Add(JSON);

            base.CreateChildControls();
            ChildControlsCreated = true;
        }

        /// <summary>
        /// Reads current value from parent web part and show that in the ddl
        /// </summary>
        public override void SyncChanges()
        {
            EnsureChildControls();
            ParentWebPart = WebPartToEdit as HDIAggregatedViewWP;
            try
            {
                if (ParentWebPart != null && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
                {

                    JSON.Text = ParentWebPart.sConfig;
                }
            }
            catch
            {
                JSON.Text += "Hmmm";
            }
        }

        /// <summary>
        /// Applies change in editor part ddl to the parent web part
        /// </summary>
        /// <returns></returns>
        public override bool ApplyChanges()
        {
            try
            {
                EnsureChildControls();
                ParentWebPart = WebPartToEdit as HDIAggregatedViewWP;

                if (ParentWebPart != null && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
                {
                    //  ParentWebPart.Config = Helper.Deserialize<Config>(JSON.Text);
                    ParentWebPart.sConfig = JSON.Text;
                }

                // The operation was succesful
                return true;
            }
            catch
            {
                // Because an error has occurred, the SyncChanges() method won’t be invoked.
                return false;
            }
        }

        private void ReferencePrevalidatedScript(string objectToValidate, string scriptPath)
        {
            Controls.Add(new LiteralControl(string.Format(
                @"<script>if (typeof ({0}) == ""undefined"") {{
                                                                document.write(""<script"" + "" src='{1}'><"" + ""/script>"");
                                                                }}
                                                            </script>",
                objectToValidate, scriptPath)));
        }

    }
}
